import { combineReducers } from 'redux';
import * as types from '@src/model/actionType';

// 首页数据统计
const decimalState = {
  BlockHeight: 0, // 子链数
  TransactionNum: 0, // 今日总交易数
  RunningTime: 0, // 运行天数
  NodeNum: 0, // 节点数
  ChainNum: 0, // 链数量
};
function decimal(state = decimalState, action) {
  switch (action.type) {
    case types.homeDecimal:
      return { ...state, ...action.payload };
    default:
      return state;
  }
}

// 最新合约列表
const latestContractState = {
  TotalCount: 2,
  GroupList: [
    {
      ContractName: 'token', // 合约名
      Timestamp: 1606669261, // 时间戳
    },
    {
      ContractName: 'token1',
      Timestamp: 1606669263,
    },
  ],
};
function latestContract(state = latestContractState, action) {
  switch (action.type) {
    case types.homeLatestContract:
      return { ...state, ...action.payload };
    default:
      return state;
  }
}

const chainState = {
  TotalCount: 2,
  GroupList: [
    {
      ChainName: 'chain1', // 链名
      Timestamp: 1606669261, // 时间戳
    },
    {
      ChainName: 'chain2',
      Timestamp: 1606669263,
    },
  ],
};
function latestChain(state = chainState, action) {
  switch (action.type) {
    case types.homeLatestChain:
      return { ...state, ...action.payload };
    default:
      return state;
  }
}

// 最新区块列表
const latestBlockListState = {
  TotalCount: 2,
  GroupList: [
    {
      Id: 1,
      BlockHeight: 5,
      TxNum: 3,
      ProposalNodeId: '',
      Timestamp: 1606669261,
    },
  ],
};
function latestBlockList(state = latestBlockListState, action) {
  switch (action.type) {
    case types.latestBlockList:
      return { ...state, ...action.payload };
    default:
      return state;
  }
}

// 最新交易列表
const latestTxListState = {
  TotalCount: 2,
  GroupList: [
    {
      Id: 1,
      TxId: '',
      BlockHash: '',
      Status: 'SUCCESS',
      Timestamp: 1606669261,
    },
  ],
};
function latestTxList(state = latestTxListState, action) {
  switch (action.type) {
    case types.latestTxList:
      return { ...state, ...action.payload };
    default:
      return state;
  }
}

// 节点信息·查看节点列表
const nodeListState = {
  TotalCount: 2,
  GroupList: [{ Id: 1, NodeId: 'chain1', NodeAddress: '/ip4/127.0.0.1/tcp/11303', BlockHeight: 19, Status: 1 }],
};
function nodeList(state = nodeListState, action) {
  switch (action.type) {
    case types.nodeList:
      return { ...state, ...action.payload };
    default:
      return state;
  }
}

const reducer = combineReducers({
  decimal,
  latestContract,
  latestChain,
  latestBlockList,
  latestTxList,
  nodeList,
});

export default reducer;
